package top.suven.http.oauth.token;


import org.apache.oltu.oauth2.common.message.types.GrantType;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName OAuthHandlerUtils
 * @Author suven.wang
 * @Description //TODO ${END}$
 * @CreateDate 2018-09-26  20:07
 * @WeeK 星期三
 * @Version v2.0
 **/
public class OAuthHandlerUtils {

    private static Map<String, Class<? extends OAuthTokenHandler>> handlers = new HashMap<>();

    static {
        handlers.put(GrantType.AUTHORIZATION_CODE.toString(), AuthorizationCodeTokenHandler.class);
        handlers.put(GrantType.PASSWORD.toString(), PasswordTokenHandler.class);
        handlers.put(GrantType.REFRESH_TOKEN.toString(), RefreshTokenHandler.class);
        handlers.put(GrantType.CLIENT_CREDENTIALS.toString(), ClientCredentialsTokenHandler.class);
    }

    public static  OAuthTokenHandler getOAuthTokenHandler(String grantType){
        Class<? extends OAuthTokenHandler> handler =  handlers.get(grantType);
        if(null != handler){
            try {
             return  handler.newInstance();
            }catch (Exception e){

            }
        }
        return null;
    }


}
